﻿using CSRedis;
using Mes.Pendant.Common;
using Mes.Pendant.Common.DtoMapping;
using Mes.Pendant.MaterialTypes;
using Mes.Pendant.MaterialTypeService.Dto;
using Mes.Pendant.ProcurementWarehousings.Dto;
using Mes.Pendant.SupplierInterface;
using Mes.Pendant.SupplierInterface.SupplierDto;
using Mes.Pendant.SupplierManagements;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace Mes.Pendant.ProcurementWarehousings
{
	public class ProcurementWarehousingService : ApplicationService, IProcurementWarehousingService
    {
        private readonly IRepository<ProcurementWarehousing, int> _repository;

        private readonly IRepository<SupplierManagement, int> _supplierRepository;

        public ProcurementWarehousingService(IRepository<ProcurementWarehousing, int> repository, IRepository<SupplierManagement, int> supplierrepository)
        {
            _repository = repository;
            _supplierRepository = supplierrepository;
        }

        private static Object obj = new Object();
        private readonly CSRedisClient redisClient = new CSRedisClient("localhost:6379");

        public async Task<List<SupplierManagement>> Getsuppernames()
        {
            var typeList = redisClient.Get<List<SupplierManagement>>("typeListKey");
            if (typeList == null)
            {
                lock (obj)
                {
                    if (typeList == null)
                    {
                        typeList = _supplierRepository.GetListAsync().Result;
                        var supp = 3000 + new Random().Next(1, 1000);
                        redisClient.Set("typeListKey", typeList, supp);
                    }
                }
            }
            return typeList;
        }

        /// <summary>
        /// 采购入库单列表
        /// </summary>
        /// <returns></returns>
        public async Task<PagedResult> GetProcurementWarehousingList(string? lbounListCode, string? lbounListName, int page = 1, int limit = 10)
        {
            var predicate = PredicateBuilder.New<ProcurementWarehousing>(true);
            if (!string.IsNullOrEmpty(lbounListCode))
            {
                predicate = predicate.And(m => m.LbounListCode.Contains(lbounListCode));
            }
            if (!string.IsNullOrEmpty(lbounListName))
            {
                predicate = predicate.And(m => m.LbounListName.Contains(lbounListName));
            }
            var dto = _repository.GetListAsync(predicate).Result;
            PagedResult result = new PagedResult();
            result.PageCount = (int)Math.Ceiling((decimal)(dto.Count == 0 ? 1 : dto.Count) / limit);
            result.PageSize = limit;
            result.CurrentPage = page;
            result.RowCount = dto.Count;
            result.Queryable = dto.Skip((page - 1) * limit).Take(limit).AsQueryable();
            return result;
        }

        public async Task<int> AddProcurementWarehousing(ProcurementWarehousingDto procurementWarehousingDto)
        {
            var lbounListCode = _repository.AnyAsync(m => m.LbounListCode == procurementWarehousingDto.LbounListCode).Result;
            if (lbounListCode)
            {
                return -1;
            }

            var lbounListName = _repository.AnyAsync(m => m.LbounListName == procurementWarehousingDto.LbounListName).Result;
            if (lbounListName)
            {
                return -1;
            }

            var procurementWarehousing = procurementWarehousingDto.MapTo<ProcurementWarehousing>();
            await _repository.InsertAsync(procurementWarehousing);
            return procurementWarehousing.Id;
        }

        public async Task<int> DeleteProcurementWarehousing(int id)
        {
            await _repository.DeleteAsync(id);
            return 1;
        }

        public async Task<int> UpdateProcurementWarehousing(ProcurementWarehousingDto procurementWarehousingDto)
        {
            var lbounListName = await _repository.AnyAsync(m => m.LbounListName == procurementWarehousingDto.LbounListName && m.Id != procurementWarehousingDto.Id);

            ProcurementWarehousing procurementWarehousing = procurementWarehousingDto.MapTo<ProcurementWarehousing>();
            await _repository.UpdateAsync(procurementWarehousing);
            return procurementWarehousing.Id;
        }

        public async Task<ProcurementWarehousingDto> GetProcurementWarehousingInfo(int id)
        {
            var entity = _repository.GetAsync(id).Result;
            var supplierManagementDto = ObjectMapper.Map<ProcurementWarehousing, ProcurementWarehousingDto>(entity);
            return supplierManagementDto;
        }
    }
}
